package com.sc.myspringbootmo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.myspringbootmo.pojo.Content;
import com.sc.myspringbootmo.pojo.Drafts;
import com.sc.myspringbootmo.service.IContentService;
import com.sc.myspringbootmo.service.IDraftsService;
import com.sc.myspringbootmo.utils.ResponseMsg;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author svblack
 * @since 2025-09-23 03:36:11
 */
@RestController
@RequestMapping("/draftBox")
public class DraftsController {
    @Autowired
    IDraftsService draftsService;

    @Autowired
    IContentService contentService;
@RequestMapping("add")
    ResponseMsg add(@RequestBody Drafts drafts) {
    if (draftsService.save(drafts)) {
        return ResponseMsg.success();

    }
    return null;
    }

    @RequestMapping("list")
    ResponseMsg list(@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "5") Integer pageSize) {

        IPage<Content> pagInfo = contentService.selectList(new Page<>(pageNum, pageSize));
        return ResponseMsg.getInstance(200,"草稿箱数据",pagInfo);

    }

    @RequestMapping("del/{contentId}")
    Boolean del(@PathVariable Integer contentId,HttpServletRequest request
    ) {
        System.out.println("请求路径：" + request.getRequestURI());
        System.out.println("接收的 contentId：" + contentId);

        if (contentId == null) {
            return false;
        }


            return contentService.removeById(contentId);
        }



    @RequestMapping("updateContent")
    Boolean updateContent(@RequestBody Content content) {
        Content upContent = new Content();
        upContent.setStatus(0);
        upContent.setTitle(content.getTitle());
        upContent.setContentType(content.getContentType());
        upContent.setTagIds(content.getTagIds());
        upContent.setContent(content.getContent());
        return contentService.updateById(upContent);
    }
    @RequestMapping("updateStatus")
    Boolean updateStatus(@RequestParam Integer contentId,@RequestParam Integer status) {
        Content content1 = contentService.getById(contentId);
        content1.setStatus(status);
        return contentService.updateById(content1);
    }


}
